package com.vik.webgallery.web;

import java.util.ArrayList;
import java.util.Properties;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vik.webgallery.beans.Gallery;
import com.vik.webgallery.beans.User;
import com.vik.webgallery.persistence.GalleryMapper;
import com.vik.webgallery.persistence.UserMapper;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	@Resource(name = "appConfig")
	private Properties appProperties;
	
	@Autowired
	public GalleryMapper galleryMapper;
	
	@Autowired
	public UserMapper userMapper;
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(value = "/")
	public String index(Model model){
		
		ArrayList<Gallery> newGalleries = galleryMapper.getLastAddedGalleries();
		
		for (Gallery g:newGalleries){
			g.setOwner(userMapper.getUserById(g.getOwnerId()));
		}
		
		ArrayList<User> newUsers = userMapper.getLastAddedUsers();
		
		model.addAttribute("galleries", newGalleries);
		model.addAttribute("users", newUsers);
			
		return "index";
	}
	
}
